//
//  YGNewFeatureViewController.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/14.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let itemCount = 4

class YGNewFeatureViewController: UICollectionViewController {
    
    // MARK: - 属性
    private let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 重写，添加collectionViewLayout
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
    }

    private func prepareCollectionView() {
        self.collectionView!.registerClass(YGNewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // 设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

}

extension YGNewFeatureViewController {
    
    // 每组有几行
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    // 创建cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YGNewFeatureViewCell
        
//        cell.backgroundColor = UIColor.blueColor()
        cell.index = indexPath.item
        
        
        return cell
    }
    
    // 滚动停止时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int (scrollView.contentOffset.x / scrollView.frame.size.width)
        //        print("index: \(index)")
        
        if index == itemCount - 1 {
            //public func cellForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewCell?
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! YGNewFeatureViewCell
            cell.startAnimation()
        }
    }
}




























